""" DigitalICS: mobile data collection tool to complete surveys with integrated multimedia

    Copyright (C) 2009. Yael Schwartzman  

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
    
    Contact information: Yael Schwartzman -  yaelsf@gmail.com
    """
import sys, traceback,time
try:import e32
except:
    import view.pseudoe32 as e32


class Logger:
    """ Simple logger to send stderr,stdout to a predefined digitalics log file"""
    def __init__(self, log_file ="e:/system/my_log.txt" ):
        if e32.in_emulator():
            log_file ="c:/system/my_log.txt"
        else:
            log_file = "e:\\digitalics\\logs\\log.txt"
        self.log_file = log_file
        
    
    def log_error(self,msg):
        """
        Write the usual traceback information to a file, followed by a listing of all the
        local variables in each frame.
        """
        t = time.strftime("%a %d/%b/%Y %H:%M:%S", time.gmtime())
        logfile = open(self.log_file, "a")
        logfile.write("Error at %s "% t)
        logfile.write(msg)
        traceback.print_exc(file=logfile)
        logfile.write( "---------------------\n")
        logfile.close()
        
    def log(self,data):
        log = open (self.log_file,"a")
        log.write( "---------------------\n")
        log.write("%s\n" %data);
        log.close()
    
    def clear_log(self):
        log = open(self.log_file, "w")
        log.close()

    def write(self, line):
        log_file = open(self.log_file, 'a')
        log_file.write(line)
        log_file.close()
    
    def writelines(self, lines):
        self.write(''.join(lines))
        
    
logger = Logger()
sys.stdout = logger
sys.stderr = logger

if __name__ == "__main__":
    my_log = Logger("c:/system/my_log.txt")
    sys.stderr = sys.stdout = my_log
    print "Testing logging"
    

 